def top_n(items, num, compare_func=None):
    """
    Get the top num items from the items.
    :param items: a list/tuple/set/dict of the items.
    :param num: number of the maximum items to be returned from the items.
    :param compare_func: the function used to compare two items. It contains
    two parameters: a and b, and returns a negitive number if a < b, zero if
    a == b, or a positive number if a > b.
    :return: a list contains top num items of the iterable.
    """
    assert num >= 0
    if num == 0:
        return ()
    items = tuple(items)
    if len(items) <= num:
        return items
    if compare_func is None:
        compare_func = lambda a, b: 1 if a > b else 0 if a == b else -1

    result = []
    while True:
        left = []
        right = []
        mid = items[-1]
        for item in items[:-1]:
            if compare_func(item, mid) < 0:
                right.append(item)
            else:
                left.append(item)
        ls = len(left)
        if ls > num:
            items = left
        elif ls == num:
            result.extend(left)
            break
        else:
            result.extend(left)
            result.append(mid)
            items = right
            num -= 1 + ls
            if num <= 0:
                break
    return result


if __name__ == '__main__':
    a = [3, 9, 7, 12, 5, 6, 10, 11, 1, 4, 5]
    for n in range(1, len(a)+1):
        print(top_n(a, n))
